<?php

namespace App\Http\Controllers;

use App\Http\Models\AirTicket;
use App\Http\Models\Order;
use App\Http\Models\Passenger;
use App\Http\Models\TicketType;
use Carbon\Carbon;
use Cassandra\Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PassengerController extends Controller
{
	/**
	 * Display a listing of the resource.
	 *
	 * @param Request $request
	 * @return  \Illuminate\Http\JsonResponse
	 */
	public function index(Request $request)
	{
		if ($request->has('limit')) {
			$items = Passenger::paginate($request->input('limit'));
		} else {
			$items = Passenger::all();
		}

		return $this->table($items);
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @param \Illuminate\Http\Request $request
	 * @return  \Illuminate\Http\JsonResponse
	 */
	public function store(Request $request)
	{
		$model = new Passenger;
		foreach ($request->toArray() as $key => $value) {
			if (in_array($key, $model->getFillable())) {
				$model[$key] = $value;
			}
		}
		return $model->save() ?
			$this->success('') : $this->failed('添加失败');
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @param \Illuminate\Http\Request $request
	 * @return  \Illuminate\Http\JsonResponse
	 */
	public function book1(Request $request)
	{
		$all_costs = 0;
		$airTicketArr = [];
		DB::enableQueryLog();
		try {
			DB::beginTransaction();
			$ticketType = TicketType::find($request->input('tid'));

			$order_id = Order::insertGetId([
				'customer_id' => session()->get('id'),
				'payment_status' => '未支付'
			]);
			$num = intval($request->input('num'));
			for ($i = 0; $i < $num; $i++) {
				$passenger = new Passenger;
				$passenger->id = $request->input('id' . $i);
				$passenger->name = $request->input('name' . $i);
				$passenger->phone = $request->input('phone' . $i);
				$sex_num = substr($passenger->id, 16, 1);
				$passenger->sex = $sex_num % 2;
				$passenger->creditStatus = '良好';
				$passenger->save();
				$airTicket = new AirTicket;
				$airTicket->passenger_id = $request->input('id' . $i);
				$airTicket->order_id = $order_id;
				$airTicket->ticketType_id = $request->input('tid');
				$airTicket->passenger_id = $passenger->id;
				$airTicket->passenger_id = $passenger->id;
				$airTicket->construction_costs = 50;
				$airTicket->insurance_costs = 0;
				$airTicket->total_costs = $ticketType->basic_price + $airTicket->construction_costs + $airTicket->insurance_costs;
				$airTicket->luggage_count = 0;
				$airTicket->meal_type = '普通餐食';
				$airTicket->book_time = Carbon::now();
				$airTicket->save();
				$all_costs += $airTicket->total_costs;
				array_push($airTicketArr, $airTicket);
			}
			Order::whereId($order_id)->update(['costs'=>$all_costs]);
			$ticketType->remaining_tickets = $ticketType->remaining_tickets - $num;
			$ticketType->save();
			DB::commit();
			session()->put('order_id', $order_id);
		} catch (Exception $e) {
			DB::rollBack();
			return $this->failed(DB::getQueryLog());
		}
		return $this->success([
			'order_id' => $order_id,
			'all_costs' => $all_costs,
			'airTickets' => $airTicketArr
		]);
	}

	/**
	 * Display the specified resource.
	 *
	 * @param int $id
	 * @return  \Illuminate\Http\JsonResponse
	 */
	public function show($id)
	{
		return $this->success(Passenger::find($id));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param \Illuminate\Http\Request $request
	 * @param int $id
	 * @return  \Illuminate\Http\JsonResponse
	 */
	public function update(Request $request, $id)
	{
		$model = Passenger::find($id);
		foreach ($request->toArray() as $key => $value) {
			if (in_array($key, $model->getFillable())) {
				$model[$key] = $value;
			}
		}
		return $model->update() ? $this->success('') : $this->failed('更新失败');
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param int $id
	 * @return  \Illuminate\Http\JsonResponse
	 */
	public function destroy($id)
	{
		return Passenger::where('id', $id)->delete() ? $this->success('') : $this->failed('删除失败');

	}
}
